package com.jxb.picker;

import android.content.Context;
import android.graphics.Color;

import com.bigkoo.pickerview.BuildConfig;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jinxb
 * date     : 2021/7/28
 * desc     :
 */
public class OptionPickerViewUtils {

    private OptionsPickerView<String> pvOptions;

    private OptionPickerViewUtils() {
    }

    public static OptionPickerViewUtils getInstance() {
        return Holder.sInstance;
    }

    private static class Holder {
        public static OptionPickerViewUtils sInstance = new OptionPickerViewUtils();
    }

    /**
     * 底部显示String类型的pickview
     */
    public void showPickViewBottom(Context context, String title, List<String> options1Items,
                                   OnOptionsSelectListener selectListener) {
        showPickView(context, title, options1Items, false, selectListener, null);
    }

    /**
     * 以dialog形式显示String类型的pickview
     */
    public void showPickViewDialog(Context context, String title, List<String> options1Items,
                                   OnOptionsSelectListener selectListener) {
        showPickView(context, title, options1Items, true, selectListener, null);
    }

    /**
     * 底部显示T类型的pickview
     */
    public <T> void showPickViewBottom(Context context, String title, List<T> options1Items,
                                       OnOptionsSelectListener selectListener,
                                       OnDataTransform<T> onDataTransform) {
        showPickView(context, title, options1Items, false, selectListener, onDataTransform);
    }

    /**
     * 以dialog形式显示pickview
     */
    public <T> void showPickViewDialog(Context context, String title, List<T> options1Items,
                                       OnOptionsSelectListener selectListener, OnDataTransform<T> onDataTransform) {
        showPickView(context, title, options1Items, true, selectListener, onDataTransform);
    }

    private <T> void showPickView(Context context, String title, List<T> options1Items, boolean isDialog,
                                  OnOptionsSelectListener<T> selectListener, OnDataTransform onDataTransform) {
        if (pvOptions != null && pvOptions.isShowing()) {
            pvOptions.dismiss();
            pvOptions = null;
        }
        OptionsPickerView<String> pvOptions =
                new OptionsPickerBuilder(context, (options1, options2, options3, v) ->
                        selectListener.onOptionsSelect(options1Items.get(options1)))
                        .setCancelText("取消")
                        .setSubmitText("确认")
                        .setTitleSize(20)
                        .setTitleText(title)
                        .isDialog(isDialog)
                        //点击屏幕，点在控件外部范围时，是否取消显示
                        .setOutSideCancelable(true)
                        //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                        .isCenterLabel(false)
                        //循环与否
                        .setCyclic(false, false, false)
                        //切换时是否还原，设置默认选中第一项。
                        .isRestoreItem(true)
                        .setTitleColor(Color.BLACK)
                        .setSubmitColor(Color.BLUE)
                        .setCancelColor(Color.BLUE)
                        .build();
        List<String> items = new ArrayList<>();
        for (int i = 0; i < options1Items.size(); i++) {
            T t = options1Items.get(i);
            String item = t instanceof String ? (String) t : onDataTransform.getShowOption(t);
            items.add(item);
        }
        //添加数据源
        pvOptions.setPicker(items);
        pvOptions.show();
    }

    public  interface OnOptionsSelectListener<T> {
        void onOptionsSelect(T t);
    }

    public interface OnDataTransform<T> {
        /**
         * 获取需要展示的选项
         */
        String getShowOption(T item);
    }
}
